
export class BarChart{
  option = {};
  constructor(options , data , widget){
    let option = JSON.parse(JSON.stringify(options))
    let legend = null;
    let values = [];
    let categorys = [];

    widget.meta.dataset.forEach(
      item => {
        item.specs.forEach(
          spec => {
            if(spec.code == 'legend'){
              legend = spec.value;
            }
            if(spec.code == 'category'){
              categorys.push(spec.value);
            }
            if(spec.code == 'value'){
              values.push(spec.value)
            }
          }
        )
      }
    )
    

    if(legend){
      if(!option['legend']){
        option['legend'] = {
          data: []
        }
      }else if(option['legend']['data']){
        option['legend']['data'] = [];
      }
    }

    let axisPropName = '';

    ['xAxis' , 'yAxis'].forEach(
      axis => {
        if(option[axis]){
          if(option[axis] instanceof Array){
            option[axis].forEach(
              item => {
                if(item.type == 'category'){
                  axisPropName = axis;
                }
              }
            )
          }else if(option[axis].type == 'category'){
            axisPropName = axis;
          }
        }
      }
    )

    if(!option['series']){
      option['series'] = [];
    }
    (data || []).forEach(
      item => {
        if(legend){
          option[legend].data.push(item[legend]);
        }
        if(axisPropName){
          categorys.forEach(
            (categoryName , index) => {
              if(option[axisPropName] instanceof Array){
                if(!option[axisPropName][index]['data']){
                  option[axisPropName][index]['data'] = [];
                }
                option[axisPropName][index]['data'].push(item[categoryName])
              }else{
                if(!option[axisPropName]['data']){
                  option[axisPropName]['data'] = [];
                }
                option[axisPropName]['data'].push(item[categoryName])
              }
            }
          )
        }
        if(values){
          values.forEach(
            (valueName , index) => {
              if(option['series'] instanceof Array){
                if(!option['series'][index]){
                  option['series'][index] = {
                    data: [],
                    type: 'bar'
                  };
                }
                if(!option['series'][index]['data']){
                  option['series'][index]['data'] = [];
                }
                option['series'][index]['type'] = 'bar';
                option['series'][index]['data'].push(item[valueName])
              }else{
                if(!option['series']['data']){
                  option['series']['data'] = [];
                }
                option['series']['type'] = 'bar';
                option['series']['data'].push(item[categoryName])
              }
            }
          )
        }
      }
    )
    this.purgeOptions(option);
    this.option = option;

  }

  purgeOptions(option){
    for(let name in option){
      if(typeof option[name] === 'object'){
        this.purgeOptions(option[name]);
      }else{
        if(option[name] === null){
          delete option[name];
        }
      }
    }
  }

}

export default {
  BarChart
}